use crossterm::event::Event;
use log::info;
use std::any::Any;
use texas_lib::*;
use tge::{context::Context, event::event_emit, game::Model, util::Rand};

pub const CARDW: usize = 7;
pub const CARDH: usize = 5;

enum BalatroState {
    Playing,
    Billing,
}

pub struct BalatroModel {
    pub rand: Rand,
    pub texas: TexasCards,
    pub pool: Vec<u16>,
    pub hand: Vec<u16>,
    pub skip: Vec<u16>,
    pub selected: Vec<u16>,
}

impl BalatroModel {
    pub fn new() -> Self {
        Self {
            rand: Rand::new(),
            texas: TexasCards::new(),
            pool: vec![],
            hand: vec![],
            skip: vec![],
            selected: vec![],
        }
    }

    pub fn shuffle_tiles(&mut self) {
        self.pool.clear();
        for i in 1..=52u16 {
            self.pool.push(i);
        }
        self.rand.shuffle(&mut self.pool);
    }

    // pub fn act(&mut self, _d: Dir, _context: &mut Context) {}
}

impl Model for BalatroModel {
    fn init(&mut self, _context: &mut Context) {
        self.rand.srand_now();
        self.shuffle_tiles();
        for _i in 0..9 {
            self.hand.push(self.pool.pop().unwrap());
        }
        event_emit("Balatro.RedrawTile");
    }

    fn handle_input(&mut self, context: &mut Context, _dt: f32) {
        let es = context.input_events.clone();
        for e in &es {
            match e {
                Event::Key(_key) => {}
                _ => {}
            }
        }
        context.input_events.clear();
    }

    fn handle_auto(&mut self, _context: &mut Context, _dt: f32) {}

    fn handle_event(&mut self, _context: &mut Context, _dt: f32) {}
    fn handle_timer(&mut self, _context: &mut Context, _dt: f32) {}

    fn as_any(&mut self) -> &mut dyn Any {
        self
    }
}
